package com.cmccgz.udacs_landlord_webapp.utils;


import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cmccgz.baselibrary.view.MyDividerItemDecoration;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.interfaces.OnRvItemClickListenter;

import java.util.List;
import java.util.Map;

public class PopWindowUtils {
    public static final String KEY_MENU_NAME="key_menu_name";
    public static PopupWindow getMenuListPW(Context context,List<Map<String,Object>> datas,OnRvItemClickListenter<Map<String,Object>>listenter){
        View contentView = LayoutInflater.from(context).inflate(R.layout.popwindow_menu, null);
        RecyclerView rv_menu = (RecyclerView) contentView.findViewById(R.id.rv_menu);
        rv_menu.setLayoutManager(new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false));
        rv_menu.addItemDecoration(new MyDividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        rv_menu.setAdapter(new MenuAdapter(context,datas,listenter));
        PopupWindow pw=new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        pw.setOutsideTouchable(true);
        pw.setBackgroundDrawable(ContextCompat.getDrawable(context,R.drawable.bg_popupwindow));
        pw.setFocusable(true);
        return pw;
    }

    private static class MenuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
        private Context context;
        private List<Map<String,Object>> datas;
        private OnRvItemClickListenter<Map<String,Object>>listenter;
        private LayoutInflater inflater;

        public MenuAdapter(Context context, List<Map<String, Object>> datas, OnRvItemClickListenter<Map<String, Object>> listenter) {
            this.context = context;
            this.datas = datas;
            this.listenter = listenter;
            inflater = LayoutInflater.from(context);
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MenuHolder(inflater.inflate(R.layout.item_popupwindow_menu,parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            final Map<String, Object> item = datas.get(position);
            ((MenuHolder) holder).tv_menuName.setText(((String) item.get(KEY_MENU_NAME)));
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listenter!=null) {
                        listenter.onItemClick(item,position, v);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return datas==null?0:datas.size();
        }

        private class MenuHolder extends RecyclerView.ViewHolder{

            private final TextView tv_menuName;

            public MenuHolder(View itemView) {
                super(itemView);
                tv_menuName = ((TextView) itemView.findViewById(R.id.tv_menuName));
            }
        }
    }
}
